<!--

    The MIT License
    Copyright (c) 2017 Rodolfo Forte

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.

-->
<!DOCTYPE html>
<html>
  <head>
    <title>Design Patterns - Abstract Factory Presentation</title>
    <meta charset="utf-8">
    <style>
      @import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
      @import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
      @import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);

      body { font-family: 'Droid Serif'; }
      h1, h2, h3 {
        font-family: 'Yanone Kaffeesatz';
        font-weight: normal;
      }
      .remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
	  
	  blockquote {
		border-left: 0.3em solid rgba(0,0,0,0.5);
		padding: 0 15px;
		font-style: italic;
	  }
	  
	  img {
		max-width:100%;
	  }
    </style>
  </head>
  <body>
    <textarea id="source">

class: center, middle

# Abstract Factory

---

# Also known as

* Kit

---

# Intent

* Provide an interface for creating families of related or dependent objects without specifying their concrete classes

---

# Explanation

* [Wikipedia](https://en.wikipedia.org/wiki/Abstract_factory_pattern) says:
  > "The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes"

<br />

* In plain words:
  * A factory that groups individual but related/dependent factories together without specifying their concrete classes;
  * A factory of factories; 

---

# Example

* In a factory that creates kingdoms, we need objects with common theme:

  * Elven kingdom needs an Elven king, Elven castle and Elven army;
  
  * Orcish kingdom needs an Orcish king, Orcish castle and Orcish army;

<br />
  
* There is a dependency between the objects in the kingdom;

---

# Diagram

* Based on the kingdom example, the diagram below showcases the different concrete factories and their concrete products:

.center[![Diagram](diagram1.png)]


---

# Diagram

* The class diagram below showcases the factory of factories;

* At runtime, we can define which Kingdom type is needed and pass it as a parameter to define which concrete KingdomFactory to instantiate;

* The concrete factory returned will then be able to produce the related objects of the specified type;
  
.center[![Diagram](diagram2.png)]

---

# Applicability

Use the Abstract Factory pattern when:

* A system should be independent of how its products are created, composed and represented;

* A system should be configured with one of multiple families of products;

* A family of related product objects is designed to be used together, and you need to enforce this constraint;

* You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations;

---

# Applicability

Use the Abstract Factory pattern when:

* The lifetime of the dependency is conceptually shorter than the lifetime of the consumer;

* You need a run-time value to construct a particular dependency;

* You want to decide which product to call from a family at runtime;

* You need to supply one or more parameters only known at run-time before you can resolve a dependency;

---

#Use Cases

* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime;
* Unit test case writing becomes much easier;

---

# Consequences

* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time

---

# Real world examples

[javax.xml.parsers.DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html)

[javax.xml.transform.TransformerFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/transform/TransformerFactory.html#newInstance--)

[javax.xml.xpath.XPathFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/xpath/XPathFactory.html#newInstance--)

---

# Credits

* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)

---

# Tutorials

* Source code http://java-design-patterns.com/patterns/abstract-factory/

    </textarea>
    <script src="https://gnab.github.io/remark/downloads/remark-latest.min.js">
    </script>
    <script>
      var slideshow = remark.create();
    </script>
  </body>
</html>